package com.applet.applet_shangcheng.controller;


import com.applet.applet_shangcheng.dao.ImagesMapper;
import com.applet.applet_shangcheng.entity.ShufflingFigure;
import com.applet.applet_shangcheng.service.ShufflingFigureService;
import com.applet.applet_shangcheng.utlis.PageResult;
import com.applet.applet_shangcheng.utlis.QueryPageBean;
import com.applet.applet_shangcheng.utlis.Result;
import com.applet.applet_shangcheng.utlis.StatusCode;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/shufflingFigure")
@CrossOrigin
@Api(value="轮播图管理",tags={"轮播图管理"})
public class ShufflingFigureController {
    private static final org.slf4j.Logger Logger = LoggerFactory.getLogger(ImageFiles.class);
    @Value("${applet.path.upload}")
    private String uploadPath;
    @Value("${applet.path.domain}")
    private String domain;
    @Autowired
    private ShufflingFigureService shufflingFigureService;

    @Autowired
    private ImagesMapper imagesMapper;
    //小程序
    @ApiOperation(value="WX_小程序获取轮播图")
    @GetMapping("/WXfindGoods")
    public Result findGoods(){
        List<ShufflingFigure> list = shufflingFigureService.findGoods();
        return new Result(true, StatusCode.OK,"商品查询成功",list);
    };
    //查询所有_有效_轮播图
   /* @PostMapping("/findAll")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = shufflingFigureService.findAll(queryPageBean.getQueryString(),queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        return pageResult;
    };*/
    //查询所有有效状态
    @ApiOperation(value="PC_获取轮播图")
    @PostMapping("/findAll")
    public PageResult findAll(@RequestBody QueryPageBean queryPageBean){
        PageResult pageResult = shufflingFigureService.findAll(queryPageBean.getQueryString(),queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        return pageResult;
    }

    //查询所有轮播图
    @ApiOperation(value="WX_根据goodsId获取轮播图")
    @GetMapping("/findById")
    public Result findById(@RequestParam("goodsId") int goodsId){
        ShufflingFigure list = shufflingFigureService.findByGoodsId(goodsId);
        return new Result(true, StatusCode.OK,"商品查询成功",list);
    };
    //添加轮播图
    @ApiOperation(value="PC_添加轮播图")
    @PostMapping("/insertGoods")
    public Result insertGoods(@RequestBody ShufflingFigure shufflingFigure){
        int goodsId = shufflingFigure.getGoodsId();
        if (shufflingFigure.getImage()!=null){
            imagesMapper.InsertImageStatus(shufflingFigure.getImage(),"1");
        }
        ShufflingFigure byGoodsId = shufflingFigureService.findByGoodsId(goodsId);
        if (byGoodsId!=null){
            return new Result(false, StatusCode.ERROR,"商品id重复");
        }
        shufflingFigure.setCreateDate(new Date());
        int result = shufflingFigureService.insertGoods(shufflingFigure);
        if (result>0){
            return new Result(true, StatusCode.OK,"添加轮播图商品成功");
        }
        return new Result(false, StatusCode.ERROR,"添加轮播图商品失败");
    };
    //删除轮播图
    @ApiOperation(value="PC_删除轮播图")
    @GetMapping("/deleteGoods/{goodsId}")
    public Result deleteGoods(@PathVariable("goodsId") int goodsId){
        ShufflingFigure byGoodsId = shufflingFigureService.findByGoodsId(goodsId);
        if (byGoodsId.getImage() != null) {
            imagesMapper.updateImageStatus(byGoodsId.getImage());
        }
        int result = shufflingFigureService.deleteGoods(goodsId);
        if (result>0){
            return new Result(true, StatusCode.OK,"删除轮播图商品成功");
        }
        return new Result(false, StatusCode.ERROR,"删除轮播图商品失败");
    };
    //修改轮播图状态
    @ApiOperation(value="PC_修改轮播图状态")
    @GetMapping("/updateStatus/{goodsId}/{status}")
    public Result updateStatus(@PathVariable("goodsId")int goodsId,
                                @PathVariable("status")int status){
        int result = shufflingFigureService.updateStatus(goodsId, status);
        if (result>0){
            return new Result(true, StatusCode.OK,"修改轮播图商品状态成功");
        }
        return new Result(false, StatusCode.ERROR,"修改轮播图商品状态失败");
    };
    //修改轮播图内容
    @ApiOperation(value="PC_修改轮播图内容")
    @PostMapping("/updateGoods")
    public Result updateGoods(@RequestBody ShufflingFigure shufflingFigure){
        shufflingFigure.setCreateDate(new Date());
        int result = shufflingFigureService.updateGoods(shufflingFigure);
        if (result>0){
            return new Result(true, StatusCode.OK,"修改轮播图商品成功");
        }
        return new Result(false, StatusCode.ERROR,"修改轮播图商品失败");
    }
}
